﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NAnt.Core;
using NAnt.Core.Types;
using NAnt.Core.Attributes;

namespace NAntExt.Functions
{
    /// <summary>
    /// NAnt Functions for <see cref="System.Environment"/>.
    /// </summary>
    [FunctionSet("environment", "Environment")]
    public class EnvironmentFunctions : FunctionSetBase
    {
        #region Public Instance Constructors

        public EnvironmentFunctions(Project project, PropertyDictionary propDict)
            : base(project, propDict)
        {
        }

        #endregion Public Instance Constructors
        
        /// <summary>
        /// Returns the value of the specified user environment variable.
        /// </summary>
        /// <param name="name">The user environment variable of which the value should be returned.</param>
        /// <returns>
        /// The value of the specified user environment variable.
        /// </returns>
        /// <exception cref="ArgumentException">User environment variable <paramref name="name" /> does not exist.</exception>
        [Function("get-user-variable")]
        public static string GetUserVariable(string name) {
            if (!UserVariableExists(name)) {
                throw new ArgumentException("User environment variable does not exist. Name = {0}", name);
            }

            return Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.User);
        }

        /// <summary>
        /// Gets a value indicating whether the specified user environment variable
        /// exists.
        /// </summary>
        /// <param name="name">The user environment variable that should be checked.</param>
        /// <returns>
        /// <see langword="true" /> if the user environment variable exists; otherwise,
        /// <see langword="false" />.
        /// </returns>
        [Function("user-variable-exists")]
        public static bool UserVariableExists(string name) {
            return (Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.User) != null);
        }


    }
}
